--[[
	Copyright (c) 2008-2009 yleaf (yaroot@gmail.com)
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
]]

local GUILD_INDEX_MAX = 12
local SMOOTH = {
	1, 0, 0,
	1, 1, 0,
	0, 1, 0,
}

local BC = {}
for k, v in pairs(LOCALIZED_CLASS_NAMES_MALE) do
	BC[v] = k
end

local function Hex(r, g, b)
	if(type(r) == 'table') then
		if(r.r) then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end
	
	if(not r or not g or not b) then
		r, g, b = 1, 1, 1
	end
	
	return format('|cff%02x%02x%02x', r*255, g*255, b*255)
end

-- http://www.wowwiki.com/ColorGradient
local function ColorGradient(perc, ...)
	if perc >= 1 then
		local r, g, b = select(select('#', ...) - 2, ...)
		return r, g, b
	elseif perc <= 0 then
		local r, g, b = ...
		return r, g, b
	end
	
	local num = select('#', ...) / 3

	local segment, relperc = math.modf(perc*(num-1))
	local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)

	return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc
end

--GuildControlGetNumRanks()
--GuildControlGetRankName(index)
local guildRankColor = setmetatable({}, {
	__index = function(t, i)
		if i then
			t[i] = {ColorGradient(i/GUILD_INDEX_MAX, unpack(SMOOTH))}
		end
		return i and t[i] or {1,1,1}
	end
})

local diffColor = setmetatable({}, {
	__index = function(t,i)
		if not i then return '' end
		local c = GetQuestDifficultyColor(i)
		if not c then return '|cffffffff' end
		t[i] = Hex(c)
		return t[i]
	end
})

local classColorHex = setmetatable({}, {
	__index = function(t,i)
		if not i then return '' end
		local CUSTOM_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
		local eclass = BC[i] or i
		local c = CUSTOM_CLASS_COLORS[eclass]
		if not c then return '|cffffffff' end
		t[i] = Hex(c)
		return t[i]
	end
})

local classColors = setmetatable({}, {
	__index = function(t,i)
		if not i then return '' end
		local CUSTOM_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
		local eclass = BC[i] or i
		local c = CUSTOM_CLASS_COLORS[eclass]
		if not c then return {1,1,1} end
		t[i] = {c.r, c.g, c.b}
		return t[i]
	end
})

if CUSTOM_CLASS_COLORS then
	local function callBack()
		wipe(classColorHex)
		wipe(classColors)
	end
	CUSTOM_CLASS_COLORS:RegisterCallback(callBack)
end


--FRIENDS_LEVEL_TEMPLATE = 'Level %d %s' -- For 'name location' in friends list
local FRIENDS_LEVEL_TEMPLATE = gsub(FRIENDS_LEVEL_TEMPLATE, '%%d', '%%s')
hooksecurefunc('FriendsList_Update', function()
	local friendOffset = FauxScrollFrame_GetOffset(FriendsFrameFriendsScrollFrame)
	local friendIndex
	local playerArea = GetRealZoneText()
	
	for i=1, FRIENDS_TO_DISPLAY, 1 do
		friendIndex = friendOffset + i
		local name, level, class, area, connected, status, note, RAF = GetFriendInfo(friendIndex)
		local nameText = getglobal('FriendsFrameFriendButton'..i..'ButtonTextName')
		local LocationText = getglobal('FriendsFrameFriendButton'..i..'ButtonTextLocation')
		local infoText = getglobal('FriendsFrameFriendButton'..i..'ButtonTextInfo')
		if not name then return end
		if connected then
			nameText:SetVertexColor(unpack(classColors[class]))
			if area == playerArea then
				area = format('|cff00ff00%s|r', area)
				LocationText:SetFormattedText(FRIENDS_LIST_TEMPLATE, area, status)
			end
			level = diffColor[level] .. level .. '|r'
			--class = classColorHex[class] .. class
			infoText:SetFormattedText(FRIENDS_LEVEL_TEMPLATE, level, class)
		else
			return
		end
	end
end)


hooksecurefunc('GuildStatus_Update', function()
	local playerArea = GetRealZoneText()
	
	if ( FriendsFrame.playerStatusFrame ) then
		local guildOffset = FauxScrollFrame_GetOffset(GuildListScrollFrame)
		local guildIndex
		
		for i=1, GUILDMEMBERS_TO_DISPLAY, 1 do
			guildIndex = guildOffset + i
			local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName = GetGuildRosterInfo(guildIndex)
			if not name then return end
			if online then
				local nameText = getglobal('GuildFrameButton'..i..'Name')
				local zoneText = getglobal('GuildFrameButton'..i..'Zone')
				local levelText = getglobal('GuildFrameButton'..i..'Level')
				local classText = getglobal('GuildFrameButton'..i..'Class')
				
				nameText:SetVertexColor(unpack(classColors[class]))
				if playerArea == zone then
					zoneText:SetFormattedText('|cff00ff00%s|r', zone)
				end
				levelText:SetText(diffColor[level] .. level)
			end
		end
	else
		local guildOffset = FauxScrollFrame_GetOffset(GuildListScrollFrame)
		local guildIndex
		
		for i=1, GUILDMEMBERS_TO_DISPLAY, 1 do
			guildIndex = guildOffset + i
			local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName = GetGuildRosterInfo(guildIndex)
			if not name then return end
			if online then
				local nameText = getglobal('GuildFrameGuildStatusButton'..i..'Name')
				nameText:SetVertexColor(unpack(classColors[class]))
				
				local rankText = getglobal('GuildFrameGuildStatusButton'..i..'Rank')
				rankText:SetVertexColor(unpack(guildRankColor[rankIndex]))
			end
		end
	end
end)


hooksecurefunc('WhoList_Update', function()
	local whoIndex
	local whoOffset = FauxScrollFrame_GetOffset(WhoListScrollFrame)
	
	local playerZone = GetRealZoneText()
	local playerGuild = GetGuildInfo'player'
	local playerRace = UnitRace'player'
	
	for i=1, WHOS_TO_DISPLAY, 1 do
		whoIndex = whoOffset + i
		local nameText = getglobal('WhoFrameButton'..i..'Name')
		local levelText = getglobal('WhoFrameButton'..i..'Level')
		local classText = getglobal('WhoFrameButton'..i..'Class')
		local variableText = getglobal('WhoFrameButton'..i..'Variable')
		
		local name, guild, level, race, class, zone, classFileName = GetWhoInfo(whoIndex)
		if not name then return end
		if zone == playerZone then
			zone = '|cff00ff00' .. zone
		end
		if guild == playerGuild then
			guild = '|cff00ff00' .. guild
		end
		if race == playerRace then
			race = '|cff00ff00' .. race
		end
		local columnTable = { zone, guild, race }
		
		nameText:SetVertexColor(unpack(classColors[class]))
		levelText:SetText(diffColor[level] .. level)
		variableText:SetText(columnTable[UIDropDownMenu_GetSelectedID(WhoFrameDropDown)])
	end
end)


hooksecurefunc('LFRBrowseFrameListButton_SetData', function(button, index)
	local name, level, areaName, className, comment, partyMembers, status, class, encountersTotal, encountersComplete, isLeader, isTank, isHealer, isDamage = SearchLFGGetResults(index)
	
	local c = class and classColors[class]
	if c then
		button.name:SetTextColor(unpack(c))
		button.class:SetTextColor(unpack(c))
	end
	if level then
		button.level:SetText(diffColor[level] .. level)
	end
end)


hooksecurefunc('WorldStateScoreFrame_Update', function()
	local inArena = IsActiveBattlefieldArena()
	for i = 1, MAX_WORLDSTATE_SCORE_BUTTONS do
		local index = FauxScrollFrame_GetOffset(WorldStateScoreScrollFrame) + i
		local name, killingBlows, honorableKills, deaths, honorGained, faction, rank, race, class, classToken, damageDone, healingDone = GetBattlefieldScore(index)
		if name then
			local text = classColorHex[classToken] .. name .. '|r'
			local buttonNameText = getglobal('WorldStateScoreButton' .. i .. 'NameText')
			buttonNameText:SetText(text)
		end
	end
end)
